/******************************************************************
 * Created by: GOh Chee Hong                                      
 * Date of Creation: 1/Jan/2009

 * 
 *                                                                
 * Description: This class is in charge of uploading file blocks
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace Pbdms.BusinessLogic.Mail
{
    public class FileUploader
    {
        private string key;
        private string filelocation;
        private int start_block;
        private int no_of_blocks;
        private int end_block;
        
        public delegate void StatusHandler(object sender, string args);
        public StatusHandler statusProcess;
        private FileStream fileToBeRead = null;
        private Boolean isUploaded = false;
        public delegate Boolean checkCancel();
        public checkCancel isCancel;
        public Boolean IsUploaded()
        {
            
                return isUploaded;
            
        }
        public int StartBlock
        {
            get
            {
                return start_block;
            }
        }
        public int EndBlock{
            get
            {
                return end_block-1;
            }
        }
        public int TotalBLocks
        {
            get
            {
                return no_of_blocks;
            }
        }
        public FileUploader()
        {

        }
        public FileUploader(string key, string filelocation, int start_block, int no_of_blocks)
        {
            this.key = key;
            this.filelocation = filelocation;
            this.start_block = start_block;
            this.no_of_blocks = no_of_blocks;
        }
        ~FileUploader(){
            if (fileToBeRead != null)
            {
                fileToBeRead.Close();
            }
        }
        public void Upload()
        {             
            try
            {
                int status = 0;
                OpenDHTLib.OpenLookUp openLookUp = new Pbdms.OpenDHTLib.OpenLookUp();
                long position = 0;
                long size = 0;
                long filelength = 0;
                fileToBeRead = new System.IO.FileStream(filelocation, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read);
                end_block=start_block+no_of_blocks;
                fileToBeRead.Position = start_block * 1024;
                for (int i = start_block; i < end_block; i++)
                {
                    if (isCancel != null)
                    {
                        if (isCancel() == true)
                        {
                            return;
                        }
                    }
                    position = fileToBeRead.Position;
                    size = 1024;                    
                    filelength=fileToBeRead.Length;
                    //check whether what size to read
                    if ((position + 1024) > filelength)
                    {
                        size = filelength - position;
                    }
                    else
                    {
                        size = 1024;
                    }
                    byte [] data= new byte[size];
                    fileToBeRead.Read(data, 0, data.Length);
                    
                    string filekey=key+ " Block No:"+ i;
                    status = openLookUp.Put(filekey, data, 604800);
                    //if not succeed
                    if (status != 0)
                    {
                        return;
                    }
                }
                isUploaded = true;                
            }
            catch
            {

            }
            finally
            {
                if (fileToBeRead != null)
                {
                    fileToBeRead.Close();
                }
            }
        }
        
    }
}
